<template>
  <div class="home">
    <el-container>
      <el-header>
        <div class="logo-img">
          <img src="../assets/heima.png" alt />
          <span>电商后台管理系统</span>
        </div>
        <el-button @click="btn" type="info" size="mini">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="collapse?'60px':'200px'">
          <div @click="arrowcli" class="arrow">|||</div>
          <el-menu
            :default-active="defaultActive"
            :collapse-transition="false"
            :collapse="collapse"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffcc99"
            :unique-opened="true"
            router
          >
            <el-submenu :index="index+''" v-for="(item, index) in list" :key="index">
              <template slot="title">
                <!-- 字体图标 -->
                <i style="margin-right:10px" :class="['iconfont',iconObj[index]]"></i>
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  @click="itemcli(item2.path)"
                  :index="item2.path"
                  v-for="(item2, index2) in item.children"
                  :key="index2"
                >
                  <i class="el-icon-menu"></i>
                  {{item2.authName}}
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>

        <router-view></router-view>

        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
           
<script>
import { home_api } from "@/api/index.js";
export default {
  data() {
    return {
      list: [],
      iconObj: {
        0: "icon-user",
        1: "icon-tijikongjian",
        2: "icon-shangpin",
        3: "icon-danju",
        4: "icon-baobiao"
      },

      //  element控制菜单栏收起还是打开
      collapse: false,

      // 2.菜单栏默认展开哪一项 (default-active:index)
      defaultActive: window.localStorage.getItem("defaultActive" || "users")
    };
  },
  methods: {
    //  1.菜单栏默认展开,给二级绑定点击事件
    itemcli(path) {
    window.localStorage.setItem('defaultActive',path)
    },

    handleOpen(key, keyPath) {
      // console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      // console.log(key, keyPath);
    },

    // 退出按钮
    btn() {
      this.$router.push("/login");
    },

    //  获取左侧菜单函数(获取左侧菜单数据)
    async getMenus() {
      const { data: res1 } = await home_api();
      // console.log(res1, 111);
      this.list = res1.data;
    },

    // 控制左侧菜单栏收起还是打开
    arrowcli() {
      this.collapse = !this.collapse;
    }
  },
  created() {
    // 获取左侧菜单数据
    this.getMenus();
  }
};
</script>

<style scoped lang="less">
.home {
  > .el-container {
    width: 100%;
    height: 100%;
    .el-header {
      display: flex;
      justify-content: space-between;
      padding-left: 0;
      background-color: #373d41;
      .logo-img {
        display: flex;
        align-items: center;

        span {
          color: #fff;
          font-size: 20px;
          margin-left: 20px;
        }
      }

      .el-button {
        height: 40px;
        margin: 10px 0;
        font-size: 15px;
      }
    }
    .el-container {
      flex: 1;
      .el-aside {
        background-color: #373d41;
        .arrow {
          color: #fff;
          background-color: #4a5064;
          font-size: 10px;
          line-height: 24px;
          text-align: center;
          letter-spacing: 0.2em;
          cursor: pointer;
        }
        .el-menu {
          border: none;
        }
      }

      .el-main {
        background-color: #eaedf1;
      }
    }
  }
}
</style>